using System.Collections.Generic;
using System.Web.Routing;
using Nop.Core;
using Nop.Core.Plugins;
using Nop.Services.Cms;
using Nop.Services.Configuration;
using Nop.Services.Localization;
using Nop.Plugin.Widgets.AllInOne.Data;
using Nop.Plugin.Widgets.AllInOne.Services;
using Nop.Web.Framework.Menu;
using Nop.Core.Domain.Customers;
using System.Linq;

namespace Nop.Plugin.Widgets.AllInOne
{
    /// <summary>
    /// PLugin
    /// </summary>
    public class AllInOnePlugin : BasePlugin, IWidgetPlugin, IAdminMenuPlugin
    {
        private readonly IAllInOneService _allInOneService;
        private readonly ISettingService _settingService;
        private readonly IWebHelper _webHelper;
        private readonly AllInOneObjectContext _objectContext;
        private readonly ILocalizationService _localizationService;
        private readonly IWorkContext _workContext;

        public AllInOnePlugin(IAllInOneService allInOneService, AllInOneObjectContext objectContext,
            ISettingService settingService, IWebHelper webHelper, ILocalizationService localizationService
            ,IWorkContext workContext)
        {
            this._allInOneService = allInOneService;
            this._settingService = settingService;
            this._webHelper = webHelper;
            this._objectContext = objectContext;
            this._localizationService = localizationService;
            this._workContext = workContext;
        }

        /// <summary>
        /// Gets widget zones where this widget should be rendered
        /// </summary>
        /// <returns>Widget zones</returns>
        public IList<string> GetWidgetZones()
        {
            return _allInOneService.GetAllInOnesWidgetZones(); 
        }

        /// <summary>
        /// Gets a route for provider configuration
        /// </summary>
        /// <param name="actionName">Action name</param>
        /// <param name="controllerName">Controller name</param>
        /// <param name="routeValues">Route values</param>
        public void GetConfigurationRoute(out string actionName, out string controllerName, out RouteValueDictionary routeValues)
        {
            actionName = "Configure";
            controllerName = "WidgetsAllInOne";
            routeValues = new RouteValueDictionary() { { "Namespaces", "Nop.Plugin.Widgets.AllInOne.Controllers" }, { "area", null } };
        }

        /// <summary>
        /// Gets a route for displaying widget
        /// </summary>
        /// <param name="widgetZone">Widget zone where it's displayed</param>
        /// <param name="actionName">Action name</param>
        /// <param name="controllerName">Controller name</param>
        /// <param name="routeValues">Route values</param>
        public void GetDisplayWidgetRoute(string widgetZone, out string actionName, out string controllerName, out RouteValueDictionary routeValues)
        {
            actionName = "PublicInfo";
            controllerName = "WidgetsAllInOne";
            routeValues = new RouteValueDictionary()
            {
                {"Namespaces", "Nop.Plugin.Widgets.AllInOne.Controllers"},
                {"area", null},
                {"widgetZone", widgetZone}
            };
        }
        
        /// <summary>
        /// Install plugin
        /// </summary>
        public override void Install()
        {
            //data
            _objectContext.Install();

            //locals
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.AllInOne.Manage", "Manage AllInOnes");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.AllInOne.Fields.Name", "Name");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.AllInOne.Fields.Name.Required", "Please provide a name.");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.AllInOne.Fields.Name.hint", "AllInOne name.");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.AllInOne.Fields.HtmlCode", "Html Code");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.AllInOne.Fields.HtmlCode.Required", "Please provide a HTML code.");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.AllInOne.Fields.HtmlCode.hint", "HTML code.");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.AllInOne.Fields.HtmlCodeExtra", "Html additional  Code");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.AllInOne.Fields.HtmlCodeExtra.Required", "Please provide a HTML code.");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.AllInOne.Fields.HtmlCodeExtra.hint", "HTML additional code not supported by TinyMCE Editor.");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.AllInOne.Fields.WidgetZone", "Widget Zone");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.AllInOne.Fields.WidgetZone.Required", "Please provide a Widget Zone");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.AllInOne.Fields.WidgetZone.hint", "Widget Zone");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.AllInOne.Fields.Published", "Published");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.AllInOne.Fields.Published.Hint", "Published");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.AllInOne.Fields.DisplayOrder", "Display order");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.AllInOne.Fields.DisplayOrder.Hint", "Display order");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.AllInOne.Fields.jsFileList", "Javascript files");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.AllInOne.Fields.jsFileList.Hint", "Add javascript files");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.AllInOne.Fields.CssFileList", "CSS files");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.AllInOne.Fields.CssFileList.Hint", "Add CSS files");

            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Allinone.Editdetails", "Edit details");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Allinone.Editdetails", "Edit details");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Allinone.BackToList", "back to list");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Allinone.Info", "Info");
            this.AddOrUpdatePluginLocaleResource("plugins.widgets.allinone.cssscripts", "CSS JavaScript files");
            
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Allinone.Addnew", "Add new");
            this.AddOrUpdatePluginLocaleResource("plugins.widgets.allinone.updated", "Record Updated");
            this.AddOrUpdatePluginLocaleResource("plugins.widgets.allinone.added", "Record Added");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Allinone.Deleted", "Record Deleted");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.AllInOne.AdminMenuText", "All in one");

            base.Install();
        }

        /// <summary>
        /// Uninstall plugin
        /// </summary>
        public override void Uninstall()
        {
            //data
            _objectContext.Uninstall();

            //locales
            this.DeletePluginLocaleResource("Plugins.Widgets.AllInOne.Manage");
            this.DeletePluginLocaleResource("Plugins.Widgets.AllInOne.Fields.Name");
            this.DeletePluginLocaleResource("Plugins.Widgets.AllInOne.Fields.Name.Required");
            this.DeletePluginLocaleResource("Plugins.Widgets.AllInOne.Fields.Name.hint");
            this.DeletePluginLocaleResource("Plugins.Widgets.AllInOne.Fields.HtmlCode");
            this.DeletePluginLocaleResource("Plugins.Widgets.AllInOne.Fields.HtmlCode.Required");
            this.DeletePluginLocaleResource("Plugins.Widgets.AllInOne.Fields.HtmlCode.hint");
            this.DeletePluginLocaleResource("Plugins.Widgets.AllInOne.Fields.HtmlCodeExtra");
            this.DeletePluginLocaleResource("Plugins.Widgets.AllInOne.Fields.HtmlCodeExtra.Required");
            this.DeletePluginLocaleResource("Plugins.Widgets.AllInOne.Fields.HtmlCodeExtra.hint");
            this.DeletePluginLocaleResource("Plugins.Widgets.AllInOne.Fields.WidgetZone");
            this.DeletePluginLocaleResource("Plugins.Widgets.AllInOne.Fields.WidgetZone.hint");
            this.DeletePluginLocaleResource("Plugins.Widgets.AllInOne.Fields.WidgetZone.Required");
            this.DeletePluginLocaleResource("Plugins.Widgets.AllInOne.Fields.Published");
            this.DeletePluginLocaleResource("Plugins.Widgets.AllInOne.Fields.Published.Hint");
            this.DeletePluginLocaleResource("Plugins.Widgets.AllInOne.Fields.DisplayOrder");
            this.DeletePluginLocaleResource("Plugins.Widgets.AllInOne.Fields.DisplayOrder.Hint");
            this.DeletePluginLocaleResource("Plugins.Widgets.AllInOne.Fields.jsFileList");
            this.DeletePluginLocaleResource("Plugins.Widgets.AllInOne.Fields.jsFileList.Hint");
            this.DeletePluginLocaleResource("Plugins.Widgets.AllInOne.Fields.CssFileList");
            this.DeletePluginLocaleResource("Plugins.Widgets.AllInOne.Fields.CssFileList.Hint");

            this.DeletePluginLocaleResource("Plugins.Widgets.Allinone.Editdetails");
            this.DeletePluginLocaleResource("Plugins.Widgets.Allinone.Editdetails");
            this.DeletePluginLocaleResource("Plugins.Widgets.Allinone.BackToList");
            this.DeletePluginLocaleResource("Plugins.Widgets.Allinone.Info");
            this.DeletePluginLocaleResource("Plugins.Widgets.Allinone.cssscripts");
            this.DeletePluginLocaleResource("Plugins.Widgets.Allinone.Addnew");
            this.DeletePluginLocaleResource("plugins.widgets.allinone.updated");
            this.DeletePluginLocaleResource("plugins.widgets.allinone.added");
            this.DeletePluginLocaleResource("Plugins.Widgets.Allinone.Deleted");
           
            this.DeletePluginLocaleResource("Plugins.Widgets.AllInOne.AdminMenuText");

            base.Uninstall();
        }

        public void ManageSiteMap(SiteMapNode rootNode)
        {
            var menuItem = new SiteMapNode()
            {
                SystemName = "Hz",
                Title = _localizationService.GetResource("Plugins.Widgets.AllInOne.AdminMenuText"),
                ControllerName = "WidgetsAllInOne",
                ActionName = "List",
                Visible = true,
                RouteValues = new RouteValueDictionary() { { "area", null } },
            };

            var pluginNode = rootNode.ChildNodes.FirstOrDefault(x => x.SystemName == "Third party plugins");
            if (pluginNode != null)
                pluginNode.ChildNodes.Add(menuItem);
            else
                rootNode.ChildNodes.Add(menuItem);
        }
    }
}
